package uit.nfcmenu.activity;

import java.util.Timer;
import java.util.TimerTask;

import uit.nfcmenu.manager.SyncManager;
import uit.nfcmenu.util.CommConstant;
import uit.nfcmenu.webservice.MyException;
import android.app.IntentService;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.util.Log;


public class BackGroundSync extends IntentService {
	private static final String tag = BackGroundSync.class.getSimpleName();

	public static final String NAME = "SERVICE";
	public String name = "service";
	static BackGroundSync me;

	public static final String TAG = "OFFLINE SERVICE";
	public static final int APP_ID = 1;
	private static boolean stop_flag = false;
	private static boolean is_running = false;
	private static boolean isProgress = false;
	Handler hanler;
	Context mContext; // reference to the application context...
	MyTimerTask syncAgent;
	Timer timer;
	SyncManager syncMgr;

	public BackGroundSync() {
		super(NAME);
		me = this;
	}

	@Override
	public void onDestroy() { }
	
	

	private class MyTimerTask extends TimerTask {
//		private final String tag = MyTimerTask.class.getSimpleName();

		@Override
		public void run() {
			Log.w(tag, "service... start");
			if (is_running || isProgress)
				return;
			is_running = true;
			try {
				SyncManager.getInstance(mContext).run();
				hanler.post(final_sync_update_view);
			} catch (MyException e) {
				Log.w(tag, "sync error" + e.getMessage());
			}
			Log.w(tag, "service... end");
			is_running = false;
		}
	}

	public static void othersIsProgress() {
		isProgress = true;
	}
	
	public static void otherEndProgress() {
		isProgress = false;
	}
	
	public static boolean getSyncStatus() {
		return is_running;
	}


	@Override
	public void onCreate() {
		mContext = getApplicationContext();
		hanler = new Handler();
		super.onCreate();
	}

	public static void stopService() {
		BaseActivity.baseActivity = null;
		stop_flag = true;
		if (me != null) {
			me.InterrupThread();
			me.resetTimer();
		}

		me = null;

	}

	public static void resetService() {
		stop_flag = false;
	}

	public void resetTimer() {
		timer.cancel();
		timer.purge();
		
		syncAgent.cancel();
		syncAgent = null;
	}

	public void InterrupThread() {
		Thread.interrupted();
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		if (syncAgent != null) {
			syncAgent.cancel();
		}
		syncAgent = new MyTimerTask();

		if (timer != null) {
			timer.cancel();
			timer.purge();
		}

		timer = new Timer();
		timer.scheduleAtFixedRate(syncAgent, CommConstant.DELAY_TIME, CommConstant.SHEDULE_TIME);
		while (!stop_flag) {
			try {
				Thread.sleep(5000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

/*	private class DisplayToast implements Runnable {
		MyException error;

		public void setError(MyException er) {
			error = er;
		}

		public void run() {
			;
		}
	}*/

	Runnable final_sync_update_view = new Runnable() {

		@Override
		public void run() {
			if (BaseActivity.baseActivity != null) {
				BaseActivity.baseActivity.notifyDataChanged();
			}
		}
	};
}
